﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Web;

namespace CSharpPost
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void exit_button_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private static bool ValidateRemoteCertificate(object sender,X509Certificate certificate,X509Chain chain,SslPolicyErrors policyErrors)
        {
            return true;
        }

        private void post_worker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                PostReqParam param = (PostReqParam)e.Argument;

                ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateRemoteCertificate);

                string postData = param.request;
                string response = string.Empty;
                string url = param.url;
                HttpWebRequest req = WebRequest.Create(new Uri(url)) as HttpWebRequest;
                req.AllowWriteStreamBuffering = true;
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                string http_enc_data = HttpUtility.UrlEncode(postData);
                string frmData = "TibetXmlRequest=" + http_enc_data;
                byte[] formData = UTF8Encoding.UTF8.GetBytes(frmData);
                req.ContentLength = formData.Length;
 
                using (Stream post = req.GetRequestStream())
                {
                    post.Write(formData, 0, formData.Length);
                    post.Close();
                }
 
                using(HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
                {
                    StreamReader reader = new StreamReader(resp.GetResponseStream());
                    string resContent=string.Empty;
                    resContent = reader.ReadToEnd();
                    response = resContent;
                }

                e.Result = response;
            }
            catch (Exception error)
            {

            }
        }

        private void post_worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                this.response_textbox.Text = e.Result.ToString();
            }

            this.main_layout.Enabled = true;
        }

        private void send_button_Click(object sender, EventArgs e)
        {
            if (this.request_textbox.Text.Length > 0)
            {
                this.main_layout.Enabled = false;

                PostReqParam param = new PostReqParam();
                param.url = this.link_textbox.Text;
                param.request = this.request_textbox.Text;
                post_worker.RunWorkerAsync(param);
            }
        }
    }

    public class PostReqParam
    {
        public string url = "";
        public string request = "";
    }
}
